<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-06 11:16
 *
 * Version: 2.0
 */

namespace App\Modules\DataCenter\Repositories;

use App\Models\VisitHistory;
use App\Modules\DataCenter\Validators\VisitHistoryValidator;
use App\Modules\System\Criterias\getVisitorListCriteria;
use Flower\Core\Repository\Eloquent\BaseRepository;

class VisitHistoryRepository extends BaseRepository
{
	public function model()
	{
		return VisitHistory::class;
	}

	public function validator()
	{
		return VisitHistoryValidator::class;
	}

	/**
	 * 访客数新增
	 * @param $requestData
	 * @return bool
	 */
	public function addVisitor($requestData)
	{
		$info = $this->model->where('created_at', 'like', date('Y-m-d', time()) . "%")->firstOrCreate($requestData);
		$info->count += 1;
		$info->visit_time = time();
		return $info->save();
	}


	/**
	 * 计算店铺当天的访问量 和 点击量
	 * @param $storeId
	 * @return array
	 */
	public function storeVisitorToday($storeId)
	{
		$res = [
			'click_nums'   => 0,
			'visitor_nums' => 0,
		];
		try {
			$infos = $this->model->Where(['store_id' => $storeId])->where('created_at', 'like', date('Y-m-d', time()) . "%");
			$res['click_nums'] = $infos->sum('count');
			$res['visitor_nums'] = $infos->count();
			return $res;
		} catch (\Exception $e) {
			return $res;
		}
	}

	/**
	 * 计算同比访客数据
	 * @param $storeId
	 * @param $date
	 * @param $compareDate
	 * @return mixed
	 */
	public function getAmountRate($storeId, $date, $compareDate)
	{
		return $this->model
			->selectRaw('date(created_at) as dates, count(id) as counts')
			->whereRaw('(store_id=' . $storeId . ' and  date(created_at)="' . $date . '")')
			->orWhereRaw('(store_id=' . $storeId . ' and date(created_at)="' . $compareDate . '") group by date(created_at)')
			->get()->toArray();
	}

	/**
	 * 转化率
	 * @param $storeId
	 * @param $date
	 * @param $compareDate
	 * @return mixed
	 */
	public function getVisitAmountRate($storeId, $date, $compareDate)
	{
		return $this->model
			->selectRaw('date(created_at) as dates, count(id) as counts')
			->whereRaw('(store_id=' . $storeId . ' and is_order=1 and date(created_at)="' . $date . '")')
			->orWhereRaw('(store_id=' . $storeId . ' and is_order=1 and date(created_at)="' . $compareDate . '") group by date(created_at)')
			->get()->toArray();
	}


	public function getList($storeId, $startDate, $endDate,$isLimit=0,$pageSize=10)
	{

	    if($isLimit){

//            $list = $this->model->with('member:id,name,face')
//                ->where('store_id', '=', $storeId)
//                ->whereDate('created_at', '>=', $endDate)
//                ->whereDate('created_at', '<=', $startDate)
//                ->orderByDesc('created_at');

           $list =  $this->pushCriteria(new getVisitorListCriteria($storeId,$startDate,$endDate,true))->get();
        }else{
	        $pageSize = empty($pageSize) ? 10 : $pageSize;
	        $list = $this->pushCriteria(new getVisitorListCriteria($storeId,$startDate,$endDate))->paginate($pageSize, ['*'], 'paginate');
        }
		return $list;
	}






	/**
	 * 访客画图的数据 店长端
	 * @param $storeId ','分隔的字符串
	 * @param $startDate
	 * @param $endDate
	 * @return array
	 * @author Happy dong
	 * @modifiedTime 2019-08-18 20:11
	 */
	public function getVisitorGraphData($storeId, $startDate, $endDate)
	{
		if ($startDate == $endDate) {
			$list = $this->model
				->selectRaw('SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 0 and DATE_FORMAT(visit_time,\'%H\') <= 3) THEN 1 ELSE 0 end) AS hour3,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 3 and DATE_FORMAT(visit_time,\'%H\')  <= 6) THEN 1 ELSE 0 end) AS hour6,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 6  and DATE_FORMAT(visit_time,\'%H\')  <= 9) THEN 1 ELSE 0 end) AS hour9,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 9  and DATE_FORMAT(visit_time,\'%H\')  <= 12) THEN 1 ELSE 0 end) AS hour12,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 12  and DATE_FORMAT(visit_time,\'%H\')  <= 15) THEN 1 ELSE 0 end) AS hour15,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 15  and DATE_FORMAT(visit_time,\'%H\')  <= 18) THEN 1 ELSE 0 end) AS hour18,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 18  and DATE_FORMAT(visit_time,\'%H\')  <= 21) THEN 1 ELSE 0 end) AS hour21,
 SUM(CASE WHEN (DATE_FORMAT(visit_time,\'%H\')  > 21  and DATE_FORMAT(visit_time,\'%H\')  <= 24) THEN 1 ELSE 0 end) AS hour24')
->whereRaw('store_id in ('.$storeId.') and date(visit_time) ="' . $startDate . '"')->first();

		} else {
			$list = $this->model
				->selectRaw('date(visit_time) as times, count(id) as visitor_count')
				->whereRaw('store_id in (' . $storeId . ') and date(visit_time) <= "' . $startDate . '" and date(visit_time) >="' . $endDate . '"  group by times')->get();
		}
		return $list->toArray();
	}



}
